package com.firebird.app.fekpsl.adapter.exam

import android.text.Html
import android.text.Html.FROM_HTML_MODE_LEGACY
import android.view.View
import android.widget.CheckBox
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.firebird.app.fekpsl.R
import com.firebird.app.fekpsl.bean.MultipleAnswer
import com.firebird.app.fekpsl.bean.QuestionTestContentBean
import com.google.gson.Gson

/**
 * Created by firebird on 2018/4/22.
 */
class MultipleAnswerAdapter : BaseQuickAdapter<MultipleAnswer, BaseViewHolder>(R.layout.exam_item_multiple_answer) {
    inline fun <reified T> Gson.fromJson(json: String) = fromJson(json, T::class.java)
    override fun convert(helper: BaseViewHolder?, item: MultipleAnswer?) {
        helper ?: return
        item ?: return
        val index=helper.layoutPosition

        helper.getView<CheckBox>(R.id.cb_multiple_answer_a).visibility = View.GONE
        helper.getView<CheckBox>(R.id.cb_multiple_answer_b).visibility = View.GONE
        helper.getView<CheckBox>(R.id.cb_multiple_answer_c).visibility = View.GONE
        helper.getView<CheckBox>(R.id.cb_multiple_answer_d).visibility = View.GONE
        helper.getView<CheckBox>(R.id.cb_multiple_answer_e).visibility = View.GONE
        helper.getView<CheckBox>(R.id.cb_multiple_answer_f).visibility = View.GONE
        helper.getView<CheckBox>(R.id.cb_multiple_answer_g).visibility = View.GONE
        val QuestionTestContent=item.content
        val bean = Gson().fromJson<QuestionTestContentBean>(QuestionTestContent)
        val titleContent=bean.titleContent
        helper.setText(R.id.tv_multiple_question_title, Html.fromHtml(titleContent,FROM_HTML_MODE_LEGACY))
        bean.questionItemObjects.forEachIndexed{ index, s ->
            val question_choice=bean.questionItemObjects.get(index).prefix + "." + bean.questionItemObjects.get(index).content
            when(index) {
                0-> {
                    helper.setText(R.id.cb_multiple_answer_a, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_a).visibility = View.VISIBLE
                }
                1-> {
                    helper.setText(R.id.cb_multiple_answer_b, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_b).visibility = View.VISIBLE
                }
                2-> {
                    helper.setText(R.id.cb_multiple_answer_c, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_c).visibility = View.VISIBLE
                }
                3-> {
                    helper.setText(R.id.cb_multiple_answer_d, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_d).visibility = View.VISIBLE
                }
                4-> {
                    helper.setText(R.id.cb_multiple_answer_e, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_e).visibility = View.VISIBLE
                }
                5-> {
                    helper.setText(R.id.cb_multiple_answer_f, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_f).visibility = View.VISIBLE
                }
                6-> {
                    helper.setText(R.id.cb_multiple_answer_g, question_choice)
                    helper.getView<CheckBox>(R.id.cb_multiple_answer_g).visibility = View.VISIBLE
                }
            }
        }
        helper.setText(R.id.tv_multiple_question_no,(index+1).toString())
    }
}
